home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1998 August / macformat-066.iso / How-to's / Foreign Files / Creator Changer 2.8.4 / Code & Resource / Creator Changer.AE.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-18  |  7.0 KB  |  237 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  *    This file contains the functions which initialize the core 
  3.  *    AppleEvents for the program.
  4.  **********************************************************************/
  5.  
  6. #include    "Creator Changer.h"
  7. #include    "Creator Changer.AE.h"
  8.  
  9.  
  10. /**********************************************************************
  11.  *    Function Install_AE_Handlers(), this function sets up the 
  12.  *    AppleEvents for the program.
  13.  **********************************************************************/
  14.  
  15. void Install_AE_Handlers(void)
  16.     {
  17.     
  18.     AEEventHandlerUPP open_ae, quit_ae, start_ae, print_ae;
  19.     
  20.     open_ae=NewAEEventHandlerProc(Open_AE);
  21.     quit_ae=NewAEEventHandlerProc(Quit_AE);
  22.     start_ae=NewAEEventHandlerProc(Start_AE);
  23.     print_ae=NewAEEventHandlerProc(Print_AE);
  24.     
  25.     AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,   open_ae,  FALSE, FALSE);
  26.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, quit_ae,  FALSE, FALSE);
  27.     AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, start_ae, FALSE, FALSE);
  28.     AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,  print_ae, FALSE, FALSE);
  29.     
  30.     }
  31.  
  32.  
  33.  
  34. /**********************************************************************
  35.  *    Function Open_AE(), this function supports what happens when a file
  36.  *    is dropped on Creator Changer.
  37.  **********************************************************************/
  38.  
  39. pascal OSErr Open_AE(AppleEvent *the_event, AppleEvent *the_reply, long ref_con)
  40.     {
  41.     
  42.     AEDescList            the_files;
  43.     long                num_of_files;
  44.         
  45.     AEGetParamDesc(the_event, keyDirectObject, typeAEList, &the_files);
  46.     AECountItems(&the_files, &num_of_files);
  47.     Done_With_Dialog=NO;
  48.     
  49.     if(!FrontWindow())
  50.         {
  51.         if((**Global).AutoChange && Option_Key_Down) Open_Auto_Change_Files(the_files, num_of_files);
  52.         else Open_Normal_Files(the_files, num_of_files);
  53.         if((**Global).DragQuit==TRUE) All_Done=TRUE;
  54.         }
  55.     else Do_Error(Multi_File_1, EMPTY_STR, Multi_File_2, Multi_File_3);
  56.     
  57.     Maintain_Menu_Items(FrontWindow());
  58.     return(noErr);
  59.     
  60.     }
  61.  
  62.  
  63.  
  64. /**********************************************************************
  65.  *    Function Open_Auto_Change_Files(), this function opens the files which are 
  66.  *    dropped on CC and when the Automatic change box is checked.  If there are files 
  67.  *    which are not specified in the types listing, then CC will open them with the 
  68.  *    normal Change Dialog.  I do this by starting another Event Loop, until
  69.  *    the user closes out the Dialog.
  70.  **********************************************************************/
  71.  
  72. void Open_Auto_Change_Files(AEDescList the_files, long num_of_files)
  73.     {
  74.     
  75.     AEKeyword            key_word;
  76.     DescType            the_type;
  77.     Size                the_size;
  78.     auto_chng            **rsrc_handle;
  79.     Str4                types[100];
  80.     short                 i, the_item=1;
  81.  
  82.     
  83.     Make_Pop_Up_Menu(Auto.menu_id, PREF_AC, Auto.num_items);
  84.     Get_Types_From_Prefs(types);
  85.     for(i=1;i<=num_of_files;i++)
  86.         {
  87.         Multiple_Files=NO;
  88.         AEGetNthPtr(&the_files, i, typeFSS, &key_word, &the_type, &The_File_Spec, sizeof(The_File_Spec), &the_size);
  89.         FSpGetFInfo(&The_File_Spec, &File_Info);
  90.         the_item=Check_File_Type(types, File_Info.fdType);
  91.         if(the_item)
  92.             {
  93.             UseResFile(CreatorChangerPref);
  94.                 rsrc_handle=(auto_chng **)GetResource(PREF_AC, The_ac_Type[the_item]);
  95.                 Set_Type_Long((**rsrc_handle).ToFile, &File_Info.fdType, NIL_PTR, NIL_PTR);
  96.                 Set_Type_Long((**rsrc_handle).ToCreator, &File_Info.fdCreator, NIL_PTR, NIL_PTR);
  97.                 ReleaseResource((Handle)rsrc_handle);
  98.             UseResFile(CreatorChangerApp);
  99.             FSpSetFInfo(&The_File_Spec, &File_Info);
  100.             Force_Finder_Update(&The_File_Spec);
  101.             }
  102.         else
  103.             {
  104.             Chng.files=the_files;
  105.             Chng.num_files=one_file;
  106.             Open_DLOG(&Chng);
  107.             
  108.             while(!Done_With_Dialog) Handle_One_Event();
  109.             }
  110.         }
  111.     AEDisposeDesc(&the_files);
  112.     
  113.     }
  114.  
  115.  
  116.  
  117. /**********************************************************************
  118.  *    Function Open_Normal_Files(), this function this function opens the files if 
  119.  *    the Automatic Change box was NOT checked.  Once again I start another Event
  120.  *    Loop until the user closes the Change Dialog.
  121.  **********************************************************************/
  122.  
  123. void Open_Normal_Files(AEDescList the_files, long num_of_files)
  124.     {
  125.     
  126.     AEKeyword            key_word;
  127.     DescType            the_type;
  128.     Size                the_size;
  129.     
  130.     if(num_of_files==1)
  131.         {
  132.         Multiple_Files=NO;
  133.         AEGetNthPtr(&the_files, 1, typeFSS, &key_word, &the_type, &The_File_Spec, sizeof(The_File_Spec), &the_size);
  134.         FSpGetFInfo(&The_File_Spec, &File_Info);
  135.         AEDisposeDesc(&the_files);
  136.         }
  137.     else Multiple_Files=YES;
  138.     
  139.     Chng.files=the_files;
  140.     Chng.num_files=num_of_files;
  141.     Open_DLOG(&Chng);
  142.     
  143.     while(!Done_With_Dialog) Handle_One_Event();
  144.     
  145.     Multiple_Files=NO;
  146.     
  147.     }
  148.  
  149.  
  150.  
  151. /**********************************************************************
  152.  *    Function Get_Types_From_Prefs(), this function gets the types from
  153.  *    preference file and stores them in an array, for later use.
  154.  **********************************************************************/
  155.  
  156. void Get_Types_From_Prefs(Str4 *the_array)
  157.     {
  158.     
  159.     short             i;
  160.     auto_chng        **the_handle;
  161.     
  162.     the_handle=(auto_chng **)NewHandle(sizeof(auto_chng));
  163.     UseResFile(CreatorChangerPref);
  164.         for(i=1;i<=Auto.num_items;i++)
  165.             {
  166.             the_handle=(auto_chng **)GetResource(PREF_AC, The_ac_Type[i]);
  167.             p_Str_Copy((**the_handle).FromFile, the_array[i]);
  168.             ReleaseResource((Handle)the_handle);
  169.             }
  170.     UseResFile(CreatorChangerApp);
  171.     
  172.     }
  173.  
  174.  
  175.  
  176. /**********************************************************************
  177.  *    Function Check_File_Type(), this function checks the current file's
  178.  *    file type against those in the types list, if the type is not there then it
  179.  *    returns BAD, else it returns the menu index at which it found the type.
  180.  **********************************************************************/
  181.  
  182. short Check_File_Type(Str4 *the_arry, OSType the_ostype)
  183.     {
  184.     
  185.     short         i;
  186.     Str4        the_text_type;
  187.     
  188.     Set_Type_Text(the_text_type, the_ostype);
  189.     
  190.     for(i=1;i<=Auto.num_items;i++) if(Comp_The_String(the_arry[i], the_text_type)) return(i);
  191.     return(BAD);
  192.     
  193.     }
  194.  
  195.  
  196.  
  197. /**********************************************************************
  198.  *    Function Quit_AE(), this function supports the 'quit' event.  All
  199.  *    that it does is set the global switch to true to quit.
  200.  **********************************************************************/
  201.  
  202. pascal OSErr Quit_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  203.     {
  204.     
  205.     All_Done=TRUE;
  206.     return(noErr);
  207.     
  208.     }
  209.  
  210.  
  211.  
  212. /**********************************************************************
  213.  *    Function Print_AE(), this function does nothing, besides report 
  214.  *    that CC does not support the print event.
  215.  **********************************************************************/
  216.  
  217. pascal OSErr Print_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  218.     {
  219.     
  220.     return(errAEEventNotHandled);
  221.     
  222.     }
  223.  
  224.  
  225.  
  226. /**********************************************************************
  227.  *    Function Start_AE(), this function controls what happens when 
  228.  *    Creator Changer is launched.
  229.  **********************************************************************/
  230.  
  231. pascal OSErr Start_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  232.     {
  233.     
  234.     Handle_File_Choice(F_OPEN_ITEM);
  235.     return(noErr);
  236.         
  237.     }